package com.pushtime.web.rest.search;

import com.alibaba.fastjson.JSONObject;
import com.pushtime.service.search.HadoopInfo;
import com.pushtime.service.search.SimpleHdfsUploadService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Map;

@RestController
@RequestMapping("/hdfs")
public class SimpleHdfsUploadResource {

    private final SimpleHdfsUploadService simpleHdfsUploadService;

    private final HadoopInfo hadoopInfo;

    public SimpleHdfsUploadResource(SimpleHdfsUploadService simpleHdfsUploadService, HadoopInfo hadoopInfo) {
        this.simpleHdfsUploadService = simpleHdfsUploadService;
        this.hadoopInfo = hadoopInfo;
    }

    @PostMapping("/upload")
    public ResponseEntity<Map<String, String>> upload(MultipartFile file) {
        Map<String, String> map = simpleHdfsUploadService.upload(file);
        return ResponseEntity.ok().body(map);
    }

    @GetMapping("/hadoopInfo")
    public JSONObject getHadoopInfo() {
        JSONObject hadoopInfo = this.hadoopInfo.getHadoopInfo();
        return hadoopInfo;
    }

}
